﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class ExtMemberController : Base
    {
        private readonly ExtMemberService _extMember;
        private readonly LogService _logService;
        public ExtMemberController(ExtMemberService extMember, LogService logService)
        {
            _extMember = extMember;
            _logService = logService;
        }

        [HttpGet]
        public async Task<IActionResult> IndexAsync(string name, int? status, int page = 1, int limit = 15, int json = 0)
        {
            if (json == 1)
            {
                Page<ExtMember> list = await _extMember.GetPageListAsync(name, page, limit, status);
                return Json(new { code = 0, data = list.Items, count = list.TotalItems });
            }
            return View();
        }

        public async Task<IActionResult> AddOrEditAsync(int? id, int edit = 0)
        {
            if (edit == 1)
            {
                ExtMember data = await _extMember.GetEditAsync(id);
                return Json(new { status = 1, data });
            }
            ViewAdminUser adminUser = await UserClaims();
            ViewBag.id = id;
            ViewBag.uid = adminUser.Id;
            return View();
        }

        [HttpPost, ActionName("CreatePost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> CreatePostAsync(ExtMember extMember)
        {
            if (ModelState.IsValid)
            {
                await _extMember.InsertAsync(extMember);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "添加成员");
                return Json(new { status = 1, msg = "添加成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost, ActionName("EditPost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> EditPost(ExtMember extMember, int id, int dyEdit)
        {
            if (dyEdit == 1)
            {
                await _extMember.UpdateAsync(extMember, id, dyEdit);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "编辑成员排序 id:" + id);
                return Json(new { status = 1, msg = "修改成功！" });
            }
            if (ModelState.IsValid)
            {
                await _extMember.UpdateAsync(extMember, id, 0);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "编辑成员 id:" + id);
                return Json(new { status = 1, msg = "修改成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            if (await _extMember.DeleteByPriKeyAsync(id) > 0)
            {
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "删除成员 id:" + id);
                return Json(new { status = 1, msg = "删除成功！" });
            }
            else
                return Json(new { status = 0, msg = "删除失败！" });
        }

    }
}
